﻿        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Drawing;
        using System.Windows.Forms;

namespace ExtendedEventsManager
{

    public class SnapshotNameForm : Form
    {
        //private SMOHelper _smoHelper;
        private Button btnCancel;
        private Button btnOK;
        private IContainer components;
        private Label label1;
        private Label label13;
        private Label label15;
        private Label label2;
        private Label label3;
        private Label label4;
        private Label label5;
        private Label lblConnection;
        private Label lblProgress;
        private Label lblServer;
        private ListViewEx lvFileNames;
        private Panel panel1;
        private Panel panel2;
        private Panel panel3;
        private Panel pnlBottom;
        private Panel pnlLeft;
        private Panel pnlProgress;
        private TabControl tabControl1;
        private TabPage tabPage1;
        private TableLayoutPanel tblLayoutLeft;
        private TreeView tvOptions;
        private TextBox txtEditor;
        private TextBox txtSnapshotName;

        internal SnapshotNameForm(string suggestedName)
        {
            //this._smoHelper = smoHelper;
            this.InitializeComponent();
            this.SnapshotName = suggestedName;
            this.Text = "Create Snapshot";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.CloseForm();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                //if (!this._smoHelper.SnapshotNameIsUnique(this.SnapshotName))
                //{
                //    MessageBox.Show("The Snapshot name you chose is not unique! Please choose another!");
               // }
                //else if (string.IsNullOrEmpty(this.SnapshotName))
                //{
                //    MessageBox.Show("Please choose a Snapshot name!");
                //}
                //else
                //{
                    Cursor.Current = Cursors.WaitCursor;
                    //this._smoHelper.CreateSnapshot(this.SnapshotName, this.GetSnapshotFileList());
                    this.CloseForm();
                //}
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
        }

        private void CloseForm()
        {
            base.Close();
            base.Dispose();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

/*        private List<DatabaseRelocatorInfo> GetRelocateFileList()
        {
            List<DatabaseRelocatorInfo> list = new List<DatabaseRelocatorInfo>();
            new Dictionary<string, DatabaseRelocatorInfo>();
            for (int i = 0; i != this.lvFileNames.Items.Count; i++)
            {
                DatabaseRelocatorInfo tag = this.lvFileNames.Items[i].Tag as DatabaseRelocatorInfo;
                if (tag != null)
                {
                    tag.NewLogicalName = this.lvFileNames.Items[i].SubItems[0].Text;
                    tag.NewPhysicalPath = this.lvFileNames.Items[i].SubItems[1].Text;
                    list.Add(tag);
                }
            }
            return list;
        }
        */
        private Dictionary<string, string> GetSnapshotFileList()
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            for (int i = 0; i != this.lvFileNames.Items.Count; i++)
            {
                //DatabaseRelocatorInfo tag = this.lvFileNames.Items[i].Tag as DatabaseRelocatorInfo;
                //if (tag != null)
               // {
               //     dictionary[tag.OriginalLogicalName] = tag.NewPhysicalPath;
               // }
            }
            return dictionary;
        }

        private void HideTabs()
        {
            this.tabControl1.Top -= this.tabControl1.ItemSize.Height;
            this.tabControl1.Height += this.tabControl1.ItemSize.Height;
            this.tabControl1.Region = new Region(new RectangleF((float)this.tabPage1.Left, (float)this.tabPage1.Top, (float)this.tabPage1.Width, (float)(this.tabPage1.Height + this.tabControl1.ItemSize.Height)));
        }

        private void InitDatabaseFilesListView()
        {
            this.lvFileNames.View = View.Details;
            this.lvFileNames.Columns.Add("Physical Paths");
            this.lvFileNames.Scrollable = true;
            this.lvFileNames.SubItemClicked += new SubItemEventHandler(this.lvFileNames_SubItemClicked);
            this.lvFileNames.SubItemEndEditing += new SubItemEndEditingEventHandler(this.lvFileNames_SubItemEndEditing);
            this.lvFileNames.DoubleClickActivation = false;
        }

        private void InitializeComponent()
        {
            this.pnlLeft = new Panel();
            this.tblLayoutLeft = new TableLayoutPanel();
            this.label1 = new Label();
            this.tvOptions = new TreeView();
            this.label2 = new Label();
            this.label3 = new Label();
            this.pnlProgress = new Panel();
            this.lblProgress = new Label();
            this.panel3 = new Panel();
            this.lblConnection = new Label();
            this.label15 = new Label();
            this.lblServer = new Label();
            this.label13 = new Label();
            this.pnlBottom = new Panel();
            this.btnOK = new Button();
            this.btnCancel = new Button();
            this.tabPage1 = new TabPage();
            this.panel2 = new Panel();
            this.txtEditor = new TextBox();
            this.lvFileNames = new ListViewEx();
            this.label5 = new Label();
            this.panel1 = new Panel();
            this.txtSnapshotName = new TextBox();
            this.label4 = new Label();
            this.tabControl1 = new TabControl();
            this.pnlLeft.SuspendLayout();
            this.tblLayoutLeft.SuspendLayout();
            this.pnlProgress.SuspendLayout();
            this.panel3.SuspendLayout();
            this.pnlBottom.SuspendLayout();
            this.tabPage1.SuspendLayout();
            this.panel2.SuspendLayout();
            this.panel1.SuspendLayout();
            this.tabControl1.SuspendLayout();
            base.SuspendLayout();
            this.pnlLeft.Controls.Add(this.tblLayoutLeft);
            this.pnlLeft.Dock = DockStyle.Left;
            this.pnlLeft.Location = new Point(0, 0);
            this.pnlLeft.Name = "pnlLeft";
            this.pnlLeft.Size = new Size(0xcc, 0x282);
            this.pnlLeft.TabIndex = 1;
            this.tblLayoutLeft.ColumnCount = 1;
            this.tblLayoutLeft.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50f));
            this.tblLayoutLeft.Controls.Add(this.label1, 0, 0);
            this.tblLayoutLeft.Controls.Add(this.tvOptions, 0, 1);
            this.tblLayoutLeft.Controls.Add(this.label2, 0, 2);
            this.tblLayoutLeft.Controls.Add(this.label3, 0, 4);
            this.tblLayoutLeft.Controls.Add(this.pnlProgress, 0, 5);
            this.tblLayoutLeft.Controls.Add(this.panel3, 0, 3);
            this.tblLayoutLeft.Dock = DockStyle.Fill;
            this.tblLayoutLeft.Location = new Point(0, 0);
            this.tblLayoutLeft.Name = "tblLayoutLeft";
            this.tblLayoutLeft.RowCount = 6;
            this.tblLayoutLeft.RowStyles.Add(new RowStyle(SizeType.Percent, 5.121951f));
            this.tblLayoutLeft.RowStyles.Add(new RowStyle(SizeType.Percent, 94.87805f));
            this.tblLayoutLeft.RowStyles.Add(new RowStyle(SizeType.Absolute, 21f));
            this.tblLayoutLeft.RowStyles.Add(new RowStyle(SizeType.Absolute, 139f));
            this.tblLayoutLeft.RowStyles.Add(new RowStyle(SizeType.Absolute, 17f));
            this.tblLayoutLeft.RowStyles.Add(new RowStyle(SizeType.Absolute, 79f));
            this.tblLayoutLeft.Size = new Size(0xcc, 0x282);
            this.tblLayoutLeft.TabIndex = 0;
            this.label1.AutoSize = true;
            this.label1.BackColor = SystemColors.GradientActiveCaption;
            this.label1.Dock = DockStyle.Fill;
            this.label1.ForeColor = SystemColors.ActiveCaptionText;
            this.label1.Location = new Point(3, 0);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0xc6, 0x13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Select a page";
            this.label1.TextAlign = ContentAlignment.MiddleLeft;
            this.tvOptions.Dock = DockStyle.Fill;
            this.tvOptions.Location = new Point(3, 0x16);
            this.tvOptions.Name = "tvOptions";
            this.tvOptions.ShowLines = false;
            this.tvOptions.ShowPlusMinus = false;
            this.tvOptions.ShowRootLines = false;
            this.tvOptions.Size = new Size(0xc6, 360);
            this.tvOptions.TabIndex = 1;
            this.tvOptions.MouseDown += new MouseEventHandler(this.tvOptions_MouseDown);
            this.label2.AutoSize = true;
            this.label2.BackColor = SystemColors.GradientActiveCaption;
            this.label2.Dock = DockStyle.Fill;
            this.label2.ForeColor = SystemColors.ActiveCaptionText;
            this.label2.Location = new Point(3, 0x181);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0xc6, 0x15);
            this.label2.TabIndex = 2;
            this.label2.Text = "Connection";
            this.label2.TextAlign = ContentAlignment.MiddleLeft;
            this.label3.AutoSize = true;
            this.label3.BackColor = SystemColors.GradientActiveCaption;
            this.label3.Dock = DockStyle.Fill;
            this.label3.ForeColor = SystemColors.ActiveCaptionText;
            this.label3.Location = new Point(3, 0x221);
            this.label3.Name = "label3";
            this.label3.Size = new Size(0xc6, 0x11);
            this.label3.TabIndex = 4;
            this.label3.Text = "Progress";
            this.label3.TextAlign = ContentAlignment.MiddleLeft;
            this.pnlProgress.BackColor = Color.White;
            this.pnlProgress.Controls.Add(this.lblProgress);
            this.pnlProgress.Dock = DockStyle.Fill;
            this.pnlProgress.Location = new Point(3, 0x235);
            this.pnlProgress.Name = "pnlProgress";
            this.pnlProgress.Size = new Size(0xc6, 0x4a);
            this.pnlProgress.TabIndex = 5;
            this.lblProgress.Anchor = AnchorStyles.Right | AnchorStyles.Left;
            this.lblProgress.AutoSize = true;
            this.lblProgress.Font = new Font("Microsoft Sans Serif", 9.25f, FontStyle.Bold, GraphicsUnit.Point, 0);
            this.lblProgress.Location = new Point(0x40, 0x2a);
            this.lblProgress.Name = "lblProgress";
            this.lblProgress.Size = new Size(0, 0x10);
            this.lblProgress.TabIndex = 4;
            this.lblProgress.TextAlign = ContentAlignment.MiddleCenter;
            this.panel3.BackColor = Color.White;
            this.panel3.Controls.Add(this.lblConnection);
            this.panel3.Controls.Add(this.label15);
            this.panel3.Controls.Add(this.lblServer);
            this.panel3.Controls.Add(this.label13);
            this.panel3.Dock = DockStyle.Fill;
            this.panel3.Location = new Point(3, 0x199);
            this.panel3.Name = "panel3";
            this.panel3.Size = new Size(0xc6, 0x85);
            this.panel3.TabIndex = 6;
            this.lblConnection.AutoSize = true;
            this.lblConnection.Location = new Point(9, 80);
            this.lblConnection.Name = "lblConnection";
            this.lblConnection.Size = new Size(60, 13);
            this.lblConnection.TabIndex = 3;
            this.lblConnection.Text = "connection";
            this.label15.AutoSize = true;
            this.label15.Location = new Point(9, 0x43);
            this.label15.Name = "label15";
            this.label15.Size = new Size(0x40, 13);
            this.label15.TabIndex = 2;
            this.label15.Text = "Connection:";
            this.lblServer.AutoSize = true;
            this.lblServer.Location = new Point(9, 0x1b);
            this.lblServer.Name = "lblServer";
            this.lblServer.Size = new Size(0x24, 13);
            this.lblServer.TabIndex = 1;
            this.lblServer.Text = "server";
            this.label13.AutoSize = true;
            this.label13.Location = new Point(9, 14);
            this.label13.Name = "label13";
            this.label13.Size = new Size(0x29, 13);
            this.label13.TabIndex = 0;
            this.label13.Text = "Server:";
            this.pnlBottom.Controls.Add(this.btnOK);
            this.pnlBottom.Controls.Add(this.btnCancel);
            this.pnlBottom.Dock = DockStyle.Bottom;
            this.pnlBottom.Location = new Point(0xcc, 0x24f);
            this.pnlBottom.Name = "pnlBottom";
            this.pnlBottom.Size = new Size(0x285, 0x33);
            this.pnlBottom.TabIndex = 0x1f;
            this.btnOK.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.btnOK.Location = new Point(0x1cb, 0x10);
            this.btnOK.Name = "btnOK";
            this.btnOK.Size = new Size(0x4b, 0x17);
            this.btnOK.TabIndex = 0x22;
            this.btnOK.Text = "OK";
            this.btnOK.UseVisualStyleBackColor = true;
            this.btnOK.Click += new EventHandler(this.btnOK_Click);
            this.btnCancel.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.btnCancel.Location = new Point(0x22b, 0x10);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(60, 0x17);
            this.btnCancel.TabIndex = 0x21;
            this.btnCancel.Text = "Cancel";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.tabPage1.Controls.Add(this.panel2);
            this.tabPage1.Controls.Add(this.panel1);
            this.tabPage1.Location = new Point(4, 0x16);
            this.tabPage1.Name = "tabPage1";
            this.tabPage1.Padding = new Padding(3);
            this.tabPage1.Size = new Size(0x27d, 0x235);
            this.tabPage1.TabIndex = 0;
            this.tabPage1.Text = "tabPage1";
            this.tabPage1.UseVisualStyleBackColor = true;
            this.panel2.Controls.Add(this.txtEditor);
            this.panel2.Controls.Add(this.lvFileNames);
            this.panel2.Controls.Add(this.label5);
            this.panel2.Dock = DockStyle.Bottom;
            this.panel2.Location = new Point(3, 0x81);
            this.panel2.Name = "panel2";
            this.panel2.Size = new Size(0x277, 0x1b1);
            this.panel2.TabIndex = 3;
            this.txtEditor.Location = new Point(0x12b, 7);
            this.txtEditor.Name = "txtEditor";
            this.txtEditor.Size = new Size(100, 20);
            this.txtEditor.TabIndex = 0x2b;
            this.txtEditor.Visible = false;
            this.lvFileNames.AllowColumnReorder = true;
            this.lvFileNames.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.lvFileNames.DoubleClickActivation = false;
            this.lvFileNames.FullRowSelect = true;
            this.lvFileNames.LabelEdit = true;
            this.lvFileNames.Location = new Point(14, 30);
            this.lvFileNames.MultiSelect = false;
            this.lvFileNames.Name = "lvFileNames";
            this.lvFileNames.Size = new Size(0x257, 0x183);
            this.lvFileNames.TabIndex = 0x23;
            this.lvFileNames.UseCompatibleStateImageBehavior = false;
            this.lvFileNames.View = View.Details;
            this.lvFileNames.SubItemClicked += new SubItemEventHandler(this.lvFileNames_SubItemClicked);
            this.label5.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.label5.AutoSize = true;
            this.label5.Location = new Point(11, 14);
            this.label5.Name = "label5";
            this.label5.Size = new Size(0x4d, 13);
            this.label5.TabIndex = 0x20;
            this.label5.Text = "Database files:";
            this.panel1.Controls.Add(this.txtSnapshotName);
            this.panel1.Controls.Add(this.label4);
            this.panel1.Dock = DockStyle.Top;
            this.panel1.Location = new Point(3, 3);
            this.panel1.Name = "panel1";
            this.panel1.Size = new Size(0x277, 0x7f);
            this.panel1.TabIndex = 2;
            this.txtSnapshotName.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.txtSnapshotName.Location = new Point(0x7f, 0x29);
            this.txtSnapshotName.Name = "txtSnapshotName";
            this.txtSnapshotName.Size = new Size(0x1e6, 20);
            this.txtSnapshotName.TabIndex = 0x27;
            this.txtSnapshotName.KeyUp += new KeyEventHandler(this.txtSnapshotName_KeyUp);
            this.label4.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.label4.AutoSize = true;
            this.label4.Location = new Point(11, 0x2c);
            this.label4.Name = "label4";
            this.label4.Size = new Size(0x56, 13);
            this.label4.TabIndex = 0x26;
            this.label4.Text = "Snapshot Name:";
            this.tabControl1.Controls.Add(this.tabPage1);
            this.tabControl1.Dock = DockStyle.Fill;
            this.tabControl1.Location = new Point(0xcc, 0);
            this.tabControl1.Name = "tabControl1";
            this.tabControl1.SelectedIndex = 0;
            this.tabControl1.Size = new Size(0x285, 0x24f);
            this.tabControl1.TabIndex = 0x20;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            //base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x351, 0x282);
            base.Controls.Add(this.tabControl1);
            base.Controls.Add(this.pnlBottom);
            base.Controls.Add(this.pnlLeft);
            this.MinimumSize = new Size(0x297, 0x291);
            base.Name = "SnapshotNameForm";
            //base.SizeGripStyle = SizeGripStyle.Show;
            this.Text = "SnapshotNameForm";
            base.Load += new EventHandler(this.SnapshotNameForm_Load);
            this.pnlLeft.ResumeLayout(false);
            this.tblLayoutLeft.ResumeLayout(false);
            this.tblLayoutLeft.PerformLayout();
            this.pnlProgress.ResumeLayout(false);
            this.pnlProgress.PerformLayout();
            this.panel3.ResumeLayout(false);
            this.panel3.PerformLayout();
            this.pnlBottom.ResumeLayout(false);
            this.tabPage1.ResumeLayout(false);
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.tabControl1.ResumeLayout(false);
            base.ResumeLayout(false);
        }

        private void InitTreeView()
        {
            this.tvOptions.Nodes.Add("Files");
            this.ShowTabPage(0);
        }

        private void lvFileNames_SubItemClicked(object sender, SubItemEventArgs e)
        {
            this.lvFileNames.StartEditing(this.txtEditor, e.Item, e.SubItem);
        }

        private void lvFileNames_SubItemEndEditing(object sender, SubItemEndEditingEventArgs e)
        {
            //DatabaseRelocatorInfo tag = e.Item.Tag as DatabaseRelocatorInfo;
            //if (tag != null)
            //{
            //    tag.NewPhysicalPath = e.DisplayText;
            //}
        }

        private void SetDataFileNames()
        {
            for (int i = 0; i != this.lvFileNames.Items.Count; i++)
            {
                //DatabaseRelocatorInfo tag = this.lvFileNames.Items[i].Tag as DatabaseRelocatorInfo;
                //if (tag == null)
                //{
                //    return;
                //}
                //tag.SetNewFileName(this.SnapshotName);
                //this.lvFileNames.Items[i].Text = tag.NewPhysicalPath;
            }
        }

        private void ShowConnectionInfo()
        {
            //this.lblConnection.Text = this._smoHelper.Connection.TrueLogin;
            //this.lblServer.Text = this._smoHelper.Server.Name;
        }

        private void ShowDataFiles()
        {
            this.lvFileNames.Items.Clear();
            //this.ShowDataFiles(this._smoHelper.GetDatabaseDataFileNames(), "Data");
        }

        private void ShowDataFiles(Dictionary<string, string> fileNames, string fileType)
        {
            foreach (string str in fileNames.Keys)
            {
                ListViewItem item = new ListViewItem(str);
                //item.Tag = new DatabaseRelocatorInfo(str, fileNames[str], fileType);
                this.lvFileNames.Items.Add(item);
            }
        }

        private void ShowTabPage(int index)
        {
            this.tabControl1.SelectedIndex = index;
        }

        private void SnapshotNameForm_Load(object sender, EventArgs e)
        {
            this.InitDatabaseFilesListView();
            this.InitTreeView();
            this.HideTabs();
            this.ShowConnectionInfo();
            this.ShowDataFiles();
            this.SetDataFileNames();
            this.lvFileNames.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        private void tvOptions_MouseDown(object sender, MouseEventArgs e)
        {
            TreeNode nodeAt = this.tvOptions.GetNodeAt(e.X, e.Y);
            if (nodeAt != null)
            {
                this.ShowTabPage(nodeAt.Index);
            }
        }

        private void txtSnapshotName_KeyUp(object sender, KeyEventArgs e)
        {
            this.SetDataFileNames();
        }

        internal string SnapshotName
        {
            get
            {
                return this.txtSnapshotName.Text.Trim();
            }
            set
            {
                this.txtSnapshotName.Text = value;
            }
        }
    }
}
